/**
 * 模态框
 */
module.exports = function ($scope, $state, $stateParams,$resource,$modal,toaster) {
  $scope.state = $stateParams.state;
  console.log($scope.state);
/* 分页
   * ========================================= */
  $scope.maxSize = 5;             //最多显示多少个按钮
  $scope.currentPage = 1;      //当前页码
  $scope.itemsPerPage = 10         //每页显示几条
  $scope.obj={};
  $scope.d={};
  $scope.searchform = {};
  $scope.getCompanyInfo = function () {
    $resource('/api/ac/ggc/reviewCompanyService/getApplyCompanyInfo', {}, {}).save({}, function(res){
        if(res.errcode != 0){ 
            toaster.error({title:"",body:res.errmsg});
            return;
        }
        console.log('查询企业信息是否完整');
        console.log(res);
        if (res.data.msg == '0') {
          if (confirm('请先完善企业信息')) {
            $state.go('app.completeinfos');
          } else {
            $state.go('app.completeinfos');
          }
        }
    });
  }
  $scope.getCompanyInfo();

  $scope.pageChanged = function () {
      var para = {
          pageNo:$scope.currentPage,
          pageSize:$scope.itemsPerPage,
      };
      para = angular.extend($scope.searchform, para);
      $resource('/api/as/ggc/reviewGuide/findInfoByuserList', {}, {}).
      save(para, function(res){			
          if(res.errcode === 0 || res.errcode === 10003){
              $scope.a=res.data.results;					
              console.log($scope.a);
              console.log('上面是申报指南列表返回值') 
              $scope.bigTotalItems = res.data.totalRecord;
          }else{
              toaster.error({title:"",body:res.errmsg});
          }
      });
  };
  $scope.pageChanged();

  $scope.search=function(){
      var dic = {
          pageNo:$scope.currentPage,
          pageSize:$scope.itemsPerPage,
      }
      $resource('', {}, {}).save(dic, function(res){
          if(res.errcode === 0 || res.errcode === 10003){
                      // console.log(res);
              $scope.a=res.data.results;
              console.log($scope.a);
              $scope.bigTotalItems = res.data.totalRecord;
          }else{
              toaster.error({title:"",body:res.errmsg});
          }
      });
  }

 
  // 详情
  $scope.toinfo= function (index) {
      $scope.item = $scope.a[index];			
      var modalInstance = $modal.open({
        template: require('../views/noticeinfo.html'),
        controller: 'noticeinfos',
        size: 'lg',
        resolve: {
          items: function () {
            return $scope.item;
          }
        }
      });
      modalInstance.result.then(function (showResult) {	
          $scope.pageChanged();
      });
  }
  // 编辑
  $scope.edit= function (index) {
      $scope.item = $scope.a[index];			
      var modalInstance = $modal.open({
        template: require('../views/noticeupdate.html'),
        controller: 'noticeupdates',
        size: 'lg',
        resolve: {
          items: function () {
            return $scope.item;
          }
        }
      });
      modalInstance.result.then(function (showResult) {	
          $scope.pageChanged();
      });
  }
  // 发布
  $scope.publish= function (index) {    
      console.log($scope.a[index].id) ;          
      if(confirm('是否发布该条公告？')){
          $resource('/api/as/ggc/reviewGuide/updateStateUp', {}, {}).save({'id':$scope.a[index].id}, function(res){			              
            if(res.errcode === 0 || res.errcode === 10003){
                alert('发布成功')			
                $scope.pageChanged();
            }else{
                alert(res.errmsg);
            }
        });
      }		
  }

    $scope.down= function (index) {    
        console.log($scope.a[index].id) ;          
        if(confirm('下线？')){
            $resource('/api/as/ggc/reviewGuide/updateDown', {}, {}).save({'id':$scope.a[index].id}, function(res){			              
                if(res.errcode === 0 || res.errcode === 10003){
                    alert('下线成功')			
                    $scope.pageChanged();
                }else{
                    alert(res.errmsg);
                }
            });
        }		
    }
};

